﻿@using System.Data
@using System.Text
@using Root.Models.Helper.DataBase
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>RunSQL</title>
</head>
<body style="text-align:center">
    @{
        if (!IsPost)
        {
            <form action="" method="post">
                <input type="text" name="connStr" value="Enter your database's connection string." style="font-family:Consolas;width:600px" /><br />
                <textarea spellcheck="false" name="sql" style="word-wrap:break-word;font-family:Consolas;width:600px;height:300px"></textarea><br />
                <input type="submit" value="执行" />
            </form><hr />
        }
        else
        {
            try
            {
                string connStr = Request["connStr"];
                string sql = Request["sql"];
                MSSQLHelper SQLHelper = new MSSQLHelper(connStr);
                if (sql.ToLower().Contains("select"))
                {
                    DataTable dt = SQLHelper.ExecuteDataTable(sql);
                    <form action="" method="post">
                        <input type="text" name="connStr" value="@Request["connStr"]" style="font-family:Consolas;width:600px" /><br />
                        <textarea spellcheck="false" name="sql" style="word-wrap: break-word; font-family: Consolas; width: 600px; height: 300px">@Request["sql"]</textarea><br />
                        <input type="submit" value="执行" />
                    </form><hr />
                    <table style="margin:0 auto" border="1">
                        <tr>
                            @foreach (DataColumn col in dt.Columns)
                            {
                                <th>@col.ColumnName</th>
                            }
                        </tr>
                        @foreach (DataRow row in dt.Rows)
                        {
                            <tr>
                                @foreach (DataColumn col in dt.Columns)
                                {
                                    <td>@row[col].ToString()</td>
                                }
                            </tr>
                        }
                    </table>
                }
                else
                {
                    int ln = SQLHelper.ExecuteNonQuery(sql);
                    <form action="" method="post">
                        <input type="text" name="connStr" value="@Request["connStr"]" style="font-family:Consolas;width:600px" /><br />
                        <textarea spellcheck="false" name="sql" style="word-wrap: break-word; font-family: Consolas; width: 600px; height: 300px">@Request["sql"]</textarea><br />
                        <input type="submit" value="执行" />
                    </form><hr />
                    <script>alert("影响了 @ln.ToString() 行");</script>
                }
            }
            catch (Exception ex)
            {
                <form action="" method="post" style="text-align:center">
                    <input type="text" name="connStr" value="@Request["connStr"]" style="font-family:Consolas;width:600px" /><br />
                    <textarea spellcheck="false" name="sql" style="word-wrap:break-word;font-family:Consolas;width:600px">@Request["sql"]</textarea><br />
                    <input type="submit" value="执行" />
                </form><hr />
                <p>500 内部服务器错误!<br /> @HttpUtility.HtmlEncode(ex.ToString())");</p>
            }
        }
    }
</body>
</html>
